/*
    openaltimeter -- an open-source altimeter for RC aircraft
    Copyright (C) 2010  Jan Steidl
    http://openaltimeter.org

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
    Based on tutorials from http://download.oracle.com/javase/tutorial/uiswing/components/componentlist.html
 */
package org.openaltimeter.desktopapp.flightmodes;

import java.awt.Color;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

class FlightModesTableModel extends AbstractTableModel 
{
	private static final long serialVersionUID = -1214780998102000150L;
	private String[] columnNames = {"Color",
                                    "Servo value (us)",
                                    ""};
    private Vector<FlightModeRecord> data;
    
    public FlightModesTableModel()
    {
    	this.data = new Vector<FlightModeRecord>();
    }
    
    public FlightModesTableModel(Vector<FlightModeRecord> data) 
    {    	
    	this.data = data;
    }

    public Color[] getColors() 
    {
    	Color[] colors = new Color[data.size()];
    	
    	for (int i = 0; i < data.size(); i++)
    		colors[i] = (Color) getValueAt(i, 0);
    	
    	return colors;
    }
    
    public int[] getModeValues() 
    {
    	int[] modeValues = new int[data.size()];
    	
    	for (int i = 0; i < data.size(); i++)
    		modeValues[i] = (Integer) getValueAt(i, 1);
    	
    	return modeValues;
    }
    
    public int getColumnCount() 
    {
        return columnNames.length;
    }

    public void addRow() 
    {
    	this.addRow(0, FlightModeSetting.getNextColor());
	}
    
    public void addRow(int servoValue, Color color)
    {
    	data.add(new FlightModeRecord(color, servoValue));
    	
    	fireTableDataChanged();
    }

	public int getRowCount() 
	{
        return data.size();
    }

    public String getColumnName(int col) 
    {    	
        return columnNames[col];
    }

    public Object getValueAt(int row, int col) 
    {
    	switch (col) 
    	{
    		case 0: return data.get(row).getColor();
    		case 1: return data.get(row).getMicroseconds();
    		case 2: return "Remove";
    		default: return null;
    	}
    }

    /*
     * JTable uses this method to determine the default renderer/
     * editor for each cell.  If we didn't implement this method,
     * then the last column would contain text ("true"/"false"),
     * rather than a check box.
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
	public Class getColumnClass(int c) 
    {
        return getValueAt(0, c).getClass();
    }

    public boolean isCellEditable(int row, int col) 
    {
    	return true;
    }

    public void setValueAt(Object value, int row, int col) 
    {
    	switch (col) 
    	{
    		case 0: 
    			data.get(row).setColor((Color) value);
    			fireTableCellUpdated(row, col);
    			break;
    		case 1: 
    			data.get(row).setMicroseconds((Integer) value);
    			fireTableCellUpdated(row, col);
    			break;
    	}
    }

	public void removeRow(int row) 
	{
		data.remove(row);
		fireTableDataChanged();
		
		for (int i = 0; i < this.getRowCount(); i++)
			fireTableCellUpdated(i, 2);
	}
	
	public void setData(Vector<FlightModeRecord> data)
	{
		this.data = data;
		fireTableDataChanged();
	}
	
	public Vector<FlightModeRecord> getData()
	{
		return this.data;
	}
}